﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Transactions;
using IssueTracker.Data;
using IssueTracker.Business.Entity;

namespace IssueTracker.Business.BO
{
    public class ProjectMemberBO
    {
 
        private ProjectMemberDataAccess da;

        public ProjectMemberBO() 
        {
            da = new ProjectMemberDataAccess();
        }


#region Business Methods
        
        /// <summary>
        /// Add a new Project Member
        /// </summary>
        /// <param name="projectMember"></param>
        /// <returns></returns>
        public ProjectMember AddNew(ProjectMember projectMember)
        {
            ProjectMember returnProjectMember;

            projectMember.CreationDate = DateTime.Today.Date;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnProjectMember = da.Create(projectMember);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }

            return returnProjectMember;

        }

        public ProjectMember UpdateProjectMember(ProjectMember projectMember)
        {
            ProjectMember returnProjectMember;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnProjectMember = da.Update(projectMember);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                
                return returnProjectMember;
            }
        }

        public void DeleteProjectMember(ProjectMember projectMember)
        {
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    da.Delete(projectMember);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<ProjectMember> ListProjectMembers()
        {
            ProjectMemberDataAccess data = new ProjectMemberDataAccess();

            IEnumerable<ProjectMember> returnList = data.List();
            return returnList;
        }

        public ProjectMember GetProjectMember(int id)
        {
            return da.Retrieve(id);
        }

        public IEnumerable<ProjectMember> ListProjectMembersByRef(int reference)
        {
            return da.ListByReferenceId(reference);
        }

        
#endregion
        
   
    }
}
